#include "layerdata.h"
#include <QTextStream>
#include <QSound>
#include <QSettings>


LayerData::LayerData()
{
	dicoLocation = "";
}


/**
  @brief	Charge le dictionnaire dans la structure dicoWords
  @return	true	si chargement ok
			false	si le fichier n'existe pas.
  */
bool LayerData::loadDico(QString _dicoLocation)
{
	bool ret = false;
	QFile dicoFile(_dicoLocation);
	QString data;
	QStringList* dataList;
	dicoWords = new WordList();
	
	if( dicoFile.exists() )
	{
		dicoLocation = _dicoLocation;
		
		if(dicoFile.open(QIODevice::ReadOnly))
		{
			/* Get string from file */
			data = dicoFile.readAll();
			dicoFile.close();
			
			/* Get string array from separators */
			QRegExp separators("(\\;|\\n)");
			dataList = new QStringList(data.split(separators));
			
			/* Construct the DICO */
			int curIdx = 0;
			Word* curWord = new Word();
			for(int i = 0; i < dataList->length(); i ++)
			{
				QString curData = dataList->at(i);
				
				if(curData.contains("@"))
				{
					curIdx = 0;
					
					*dicoWords<<*curWord;
					
					curWord					= new Word();
				}
				else
				{
					switch(curIdx%e_dico_structure_enumSize)
					{
					case(e_dico_structure_word):
						*(curWord->word)<<curData;
						break;
					case(e_dico_structure_translation):
						*(curWord->translation)<<curData;
						break;
					default:
						
						break;
					}
					curIdx++;
				}

			}
			ret = true;
		}
		
	}
	
	return ret;
}


/**
  @brief	Retourne la liste des dico presents dans le repertoire source
  @return	QStringList contenant les differents dico trouves
 */
QStringList LayerData::getDicos()
{
	QDir myPath(QDir::currentPath());
	QStringList curFile = myPath.entryList(QDir::Files);
	QStringList foundFiles;
	
	for(int i = 0; i < curFile.length(); i++)
	{
		if(curFile.at(i).contains(".csv"))
			foundFiles<<curFile.at(i);
	}
	
	return foundFiles;
}


/**
  @brief	Retourne un mot par son indice
  @return	Word contenant le mot courant contenant sa(ses) traduction(s) ainsi que son(ses) synonyme(s)
*/
Word LayerData::getWord(int idx)
{
	return dicoWords->at(idx);
}


/** 
  @brief	Supprime un mot du dico et re ecrit le dico
  @return	true	si l'ecriture a fonctionne
			false	si non
*/
bool LayerData::DeleteWord(int idx)
{
	bool ret = false;
	
	dicoWords->removeAt(idx);
	ret = reWriteDicoFromLocal();
	
	return ret;
}


void LayerData::ClearDico()
{
	dicoWords->clear();
}


/**
	@brief	Ecrase le dico *.csv par le dico local "dicoWords"
	@return	true	si l'ecriture est ok
			false	si non
  */
bool LayerData::reWriteDicoFromLocal()
{
	bool ret = false;
	
	if(dicoLocation != "")
	{
		QFile curFile(dicoLocation);
	
		/* Open the file to add the word at the end of the file */
		curFile.open(QIODevice::WriteOnly|QIODevice::Text);
		QTextStream fileOut(&curFile);
		
		for(int i = 0; i < dicoWords->length(); i++)
			fileOut<<dicoWords->at(i).word->at(0) + ";" + dicoWords->at(i).translation->at(0) + ";" + "@\n";
		
		curFile.close();
	
		ret = true;
	}
	
	return ret;
}


/**
  @brief	Ajoute un mot a la fin de la liste et l'enregistre a la fin du fichier
  @return	true	si l'operation c'est bien passee
			false	dans le cas contraire
*/
bool LayerData::AddWord(Word newWord)
{
	bool ret = false;
	
	if(dicoLocation != "")
	{
		QFile curFile(dicoLocation);
		
		/* Add the word at the end of the word list */
		*dicoWords<<newWord;
	
		/* Open the file to add the word at the end of the file */
		curFile.open(QIODevice::Append|QIODevice::Text);
		QTextStream fileOut(&curFile);
		fileOut<<newWord.word->at(0) + ";" + newWord.translation->at(0) + ";" + "@\n";
		curFile.close();
	
		ret = true;
	}
	
	return ret;
	
}


int LayerData::getDicoLength()
{

	return dicoWords->length();
}


void LayerData::SaveDico(QString _dicoLocation)
{
	dicoLocation = _dicoLocation;
	
	QFile dicoFile(_dicoLocation);
	dicoFile.open(QIODevice::WriteOnly|QIODevice::Text);
	dicoFile.close();
	
}


void LayerData::PlaySound(QString soundName)
{
	QFile soundFile(soundName);
	
	if(soundFile.exists())
		QSound::play(soundName);
}


void LayerData::SaveIniSetting(QString settingName, QString settingValue)
{
	QSettings settings("./settings.ini",QSettings::IniFormat);
	
	settings.beginGroup("SETTINGS");
	settings.setValue(settingName, settingValue);
	settings.endGroup();
}


QString LayerData::LoadIniSetting(QString settingName)
{
	QString ret = "";
	QSettings settings("./settings.ini",QSettings::IniFormat);
	
	settings.beginGroup("SETTINGS");
	ret = settings.value(settingName).toString();
	settings.endGroup();
	
	return ret;
}


LayerData::~LayerData()
{


}
